let logger = require("pomelo-logger").getLogger(__filename);
let BasePlayer = require("../base/yuanbaoPlayer");
let util = require("util");

let Player = function (cfg, frontendId, sessionId) {
    BasePlayer.call(this, cfg, frontendId, sessionId);
    this.cards = "";
    this.allInValue = 0;
    this.blindFlag = 1;
    this.foldFlag = 0;
    this.compareLoseFlag = 0;    //比牌失败
    this.compareOpponentUid = 0;//比牌对手d的uid
    this.compareFlag = 0;       // -1 被比，1 比
    this.betChips = 0;
    this.betTimes = 0;
};

util.inherits(Player, BasePlayer);

Player.prototype.resetInTable = function () {
    this.cards = "";
    this.blindFlag = 1;
    this.foldFlag = 0;
    this.compareLoseFlag = 0;
    this.compareOpponentUid = 0;
    this.compareFlag = 0;
    this.betChips = 0;
    this.betTimes = 0;
    this.allInValue = 0;
};

Player.prototype.getAllInValue = function () {
    return this.allInValue;
};

Player.prototype.setAllInValue = function (value) {
    this.allInValue = value;
};

Player.prototype.start = function (cards, ante, fee) {
    this.resetInTable();
    this.cards = cards;
    this.balance  = this.balance - fee;
    this.betChips = ante;
};

Player.prototype.finish = function (score) {
    this.balance = this.balance + score;
};

Player.prototype.getCards = function () {
    return this.cards;
};

Player.prototype.getDisplayCards = function () {
    if (this.cards.length === 0) {
        return "";
    }
    if (this.isBlind()) {
        return "";
    } else {
        return this.cards;
    }
};

Player.prototype.getBetChips = function () {
    return this.betChips;
};

Player.prototype.getBetTimes = function () {
    return this.betTimes;
};

Player.prototype.seeCard = function () {
    this.blindFlag = 0;
};

Player.prototype.isBlind = function () {
    return this.blindFlag > 0;
};

Player.prototype.getNetBalance = function () {
    return this.balance - this.betChips;
};

Player.prototype.bet = function (amount) {
    this.betChips += amount;
    this.betTimes += 1;
};

Player.prototype.fold = function () {
    this.foldFlag = 1;
};

Player.prototype.isAlive = function () {
    return (!this.compareLoseFlag) && (!this.foldFlag) &&  this.readyFlag > 0;
};

Player.prototype.win = function (uid, compareFlag) {
    this.compareOpponentUid = uid;
    this.compareFlag = compareFlag;
};

Player.prototype.lose = function (uid, compareFlag) {
    this.compareLoseFlag = 1;
    this.compareOpponentUid = uid;
    this.compareFlag = compareFlag;
};

Player.prototype.toJSON = function () {
    return {
        account: this.account,
        position: this.position,
        balance: this.balance,
        isOnline: this.onlineFlag,
        readyFlag: this.readyFlag,
        blindFlag: this.blindFlag,
        foldFlag: this.foldFlag,
        compareLostFlag: this.compareLoseFlag,
        betChips: this.betChips,
        betTimes: this.betTimes,
        allInValue: this.allInValue
    };
};

exports.create = function (cfg, frontendId, sessionId) {
    let player = new Player(cfg, frontendId, sessionId);
    player.resetInTable();
    return player;
};
